<?php
namespace App\Repository;
use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use App\Models\Account\CustomerAccountRechargeLog;
use App\Models\Base\ApiLog;
use Illuminate\Support\Facades\Log;

/**
 * 系统日志数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class ApiLogRepository extends BaseRepository
{
    /**
     * 保存日志
     *
     * @param array $data
     * @return
     */
    public function save(array $data)
    {
        $apiLog = new ApiLog();
        foreach ($data as $field => $val)
        {
            $apiLog->{$field} = $val;
        }
        $apiLog->save();
    }

    /**
     * 日志列表
     *
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $filter = [], array $columns = ['*'])
    {
        $query = ApiLog::query()->select($columns)->orderByDesc('created_at');
        if(isset($filter['status'])) {
            $query->where('status', $filter['status']);
        }
        if(isset($filter['api_url'])) {
            $query->where('api_url', '%' . $filter['api_url'] . '%');
        }
        if(!empty($filter['time_range'][0])) {
            $query->where('created_at', '>=', $filter['time_range'][0]);
        }
        if(!empty($filter['time_range'][1])) {
            $query->where('created_at', '<=', $filter['time_range'][1]);
        }
        if(isset($filter['title'])) {
            $query->where('title', '%' . $filter['title'] . '%');
        }
        return $query;
    }
}
